package com.itcaius.weblog.admin.event.subscriber;

import com.itcaius.weblog.admin.event.PublishArticleEvent;
import com.itcaius.weblog.admin.service.AdminStatisticsService;
import com.itcaius.weblog.common.constants.Constants;
import com.itcaius.weblog.common.domain.dos.ArticleContentDO;
import com.itcaius.weblog.common.domain.dos.ArticleDO;
import com.itcaius.weblog.common.domain.mapper.ArticleContentMapper;
import com.itcaius.weblog.common.domain.mapper.ArticleMapper;
import com.itcaius.weblog.search.LuceneHelper;
import com.itcaius.weblog.search.index.ArticleIndex;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author Caius
 * @description
 * @since Created in 2025-06-12
 */
@Component
@Slf4j
public class PublishArticleSubscriber implements ApplicationListener<PublishArticleEvent> {

    @Autowired
    private LuceneHelper luceneHelper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleContentMapper articleContentMapper;
    @Autowired
    private AdminStatisticsService adminStatisticsService;

    @Override
    @Async("threadPoolTaskExecutor")
    public void onApplicationEvent(PublishArticleEvent event) {
        Long articleId = event.getArticleId();

        String threadName = Thread.currentThread().getName();

        log.info("==> threadName: {}", threadName);
        log.info("==> 文章发布事件消费成功，articleId: {}", articleId);

        ArticleDO articleDO = articleMapper.selectById(articleId);
        ArticleContentDO articleContentDO = articleContentMapper.selectByArticleId(articleId);

        Document document = new Document();
        document.add(new TextField(ArticleIndex.COLUMN_ID, String.valueOf(articleId), Field.Store.YES));
        document.add(new TextField(ArticleIndex.COLUMN_TITLE, articleDO.getTitle(), Field.Store.YES));
        document.add(new TextField(ArticleIndex.COLUMN_COVER, articleDO.getCover(), Field.Store.YES));
        document.add(new TextField(ArticleIndex.COLUMN_SUMMARY, articleDO.getSummary(), Field.Store.YES));
        document.add(new TextField(ArticleIndex.COLUMN_CONTENT, articleContentDO.getContent(), Field.Store.YES));
        document.add(new TextField(ArticleIndex.COLUMN_CREATE_TIME, Constants.DATE_TIME_FORMATTER.format(articleDO.getCreateTime()), Field.Store.YES));

        long count = luceneHelper.addDocument(ArticleIndex.NAME, document);

        log.info("==> 添加文章对应 Lucene 文档结束，articleId: {}，受影响行数: {}", articleId, count);

        adminStatisticsService.statisticsCategoryArticleTotal();
        log.info("==> 统计分类下的文章数量结束...");

        adminStatisticsService.statisticsTagArticleTotal();
        log.info("==> 统计标签下的文章数量结束...");
    }
}
